package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.springboot.entity.LeaveStu;
import com.example.springboot.entity.Student;
import com.example.springboot.mapper.LeaveStuMapper;
import com.example.springboot.mapper.StudentMapper;
import com.example.springboot.service.LeaveStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class LeaveStuServiceImpl extends ServiceImpl<LeaveStuMapper, LeaveStu> implements LeaveStuService {


    /**
     * 注入DAO层对象
     */

    @Resource
    private LeaveStuMapper leaveStuMapper;

    @Override
    public int addNewLeaveStu(LeaveStu leaveStu) {
        leaveStu.setIsagree(2);

//        String timeString = leaveStu.getApply();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//        LocalDateTime dateTime = LocalDateTime.parse(timeString, formatter);
//
//        System.out.println(dateTime.toInstant(ZoneOffset.UTC));

        return leaveStuMapper.insert(leaveStu);
    }

    @Override
    public int agreeAccount(String username,String isagree) {
        return leaveStuMapper.agreeAccount(username, isagree);
    }

    @Override
    public int delete(String username) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        return leaveStuMapper.delete(queryWrapper);
    }

    @Override
    public List<LeaveStu> findByUserName(String username,Integer pageNum, Integer pageSize, String search) {
        List<LeaveStu> LeaveStuPage = leaveStuMapper.findByUserName(username,search, pageNum,pageSize);

//        for(LeaveStu borrow : LeaveStuPage) {
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            if(borrow.getApply() != null) borrow.setApplystr(simpleDateFormat.format(borrow.getApply()));
//        }
        return LeaveStuPage;
    }

    @Override
    public int leaveNum() {
        QueryWrapper<LeaveStu> qw = new QueryWrapper<>();
        qw.isNotNull("id");
        int leaveNum = Math.toIntExact(leaveStuMapper.selectCount(qw));
        return leaveNum;
    }


}
